package com.android.view.fragment.info;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.android.view.fragment.markets.BTCFragment;
import com.android.view.fragment.markets.ETHFragment;
import com.android.view.fragment.markets.Ex55Fragment;
import com.android.R;
import com.android.view.fragment.base.BaseFragment;
import com.android.view.fragment.markets.USDTFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description:
 */

public class MarketsFragment extends BaseFragment {

    @BindView(R.id.tabLayout)
    TabLayout mTab;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

//    @BindView(R.id.srlMarkets)
//    SwipeRefreshLayout srlMarkets;

    @BindColor(R.color.lite_blue)
    int colorBlue;
    @BindColor(R.color.green)
    int colorGreen;
    @BindColor(R.color.orange)
    int colorOrange;

    List<Fragment> listFragment;
    MarketsPagerAdapter fragmentAdapter;

    public static MarketsFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        MarketsFragment fragment = new MarketsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_markets;
    }

    @Override
    protected void initView() {
        //设置下拉刷新的圆环变化颜色，蓝绿橙
//        srlMarkets.setColorSchemeColors(colorBlue, colorGreen, colorOrange);
        initViewPager();
    }

    @Override
    protected void obtainData() {
        mTab.setupWithViewPager(mViewPager);
    }

    @Override
    protected void initEvent() {
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initViewPager() {
        listFragment = new ArrayList<>();
        listFragment.add(Ex55Fragment.newInstance(1));
        listFragment.add(USDTFragment.newInstance(1));
        listFragment.add(BTCFragment.newInstance(1));
        listFragment.add(ETHFragment.newInstance(1));

        fragmentAdapter = new MarketsPagerAdapter(getFragmentManager(), listFragment);
        mViewPager.setAdapter(fragmentAdapter);
//        mViewPager.addOnPageChangeListener(new MyPageChangeListener());
        mViewPager.setOffscreenPageLimit(4);
    }

//    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
//
//        @Override
//        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//        }
//
//        @Override
//        public void onPageSelected(int position) {
//
//        }
//
//        @Override
//        public void onPageScrollStateChanged(int state) {
//        }
//    }

    private class MarketsPagerAdapter extends FragmentStatePagerAdapter {
        public String[] mTilte;
        private List<Fragment> fragments;
        public MarketsPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            fragments = fragmentList;
            mTilte = getResources().getStringArray(R.array.tab_short_Title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTilte[position];
        }

        @Override
        public BaseFragment getItem(int position) {
            return (BaseFragment) fragments.get(position);
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

}
